//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.goldhuman.IO;

import com.goldhuman.Common.Conf;
import com.goldhuman.IO.NetIO.NetSession;
import com.goldhuman.IO.NetIO.StreamIO;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class PassiveIO extends PollIO {
    boolean closing;
    NetSession assoc_session;

    protected int UpdateEvent() {
        return this.closing ? -1 : 16;
    }

    protected void PollAccept() {
        try {
            SocketChannel sc = ((ServerSocketChannel)this.channel).accept();
            if (sc != null) {
                NetSession ns = (NetSession)this.assoc_session.clone();
                ns.setPeerAddress(sc.socket().getRemoteSocketAddress());
                register(new StreamIO(sc, ns));
                sc.socket().setKeepAlive(true);
            }
        } catch (Exception var3) {
        }

    }

    private PassiveIO(ServerSocketChannel ssc, NetSession session) {
        super(ssc);
        (this.assoc_session = session).LoadConfig();
        this.closing = false;
    }

    public boolean Close() {
        return this.closing = true;
    }

    public static PassiveIO Open(NetSession assoc_session) {
        Conf conf = Conf.GetInstance();
        String section = assoc_session.Identification();
        String type = conf.find(section, "type");
        if (type.compareToIgnoreCase("tcp") == 0) {
            try {
                ServerSocketChannel ssc = ServerSocketChannel.open();
                InetSocketAddress sa = null;

                try {
                    sa = new InetSocketAddress(InetAddress.getByName(conf.find(section, "address")), Integer.parseInt(conf.find(section, "port")));
                } catch (Exception var9) {
                }

                ServerSocket ss = ssc.socket();

                try {
                    ss.setReuseAddress(true);
                    ss.setReceiveBufferSize(Integer.parseInt(conf.find(section, "so_rcvbuf")));
                } catch (Exception var8) {
                }

                ss.bind(assoc_session.OnCheckAddress(sa), Integer.parseInt(conf.find(section, "listen_backlog")));
                return (PassiveIO)register(new PassiveIO(ssc, assoc_session));
            } catch (Exception var10) {
                var10.printStackTrace();
            }
        }

        return null;
    }
}
